package com.ruoyi.controller.system;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.vo.AjaxResult;
import com.ruoyi.controller.BaseController;
import com.ruoyi.domain.SysDictData;
import com.ruoyi.service.SysDictDataService;
import com.ruoyi.service.SysDictTypeService;

@RequestMapping("system/dict/data")
@RestController
public class SysDictDataController extends BaseController{
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private SysDictTypeService sysDictTypeService;
	
	@Autowired
	private SysDictDataService sysDictDataService;
	/**
	 * 获取字典数据列表
	 * @param dictData
	 * @return
	 */
	@GetMapping("list")
	public TableDataInfo list(SysDictData dictData) {
		logger.info("获取字典数据列表,dictData:{}",dictData);
		startPage();
		List<SysDictData> list = sysDictDataService.selectDictDataList(dictData);
		return getDataTable(list);
	}
	
	/**
	 * 根据字典类型查询字典数据信息
	 * @param dictType
	 * @return
	 */
	@GetMapping("/type/{dictType}")
	public AjaxResult  dictType(@PathVariable String dictType) {
		logger.info("根据字典类型查询字典数据信息,dictType:{}",dictType);
		List<SysDictData> data = sysDictTypeService.selectDictDataByType(dictType);
		return AjaxResult.success(data);
	}
	
	/**
	 * 查询字典数据详细
	 * @param dictCode
	 * @return
	 */
	@GetMapping(value = "/{dictCode}")
	public AjaxResult getInfo(@PathVariable Long dictCode) {
		logger.info("查询字典数据详细,dictCode:{}",dictCode);
		return AjaxResult.success(sysDictDataService.selectDictDataById(dictCode));
	}
	
	/**
	 * 新增字典数据
	 * @param dict
	 * @return
	 */
	@PostMapping
	public AjaxResult add(@Validated @RequestBody SysDictData dict) {
		logger.info("新增字典数据,dict:{}",dict);
		dict.setCreateBy(getUsername());
		return toAjax(sysDictDataService.insertDictData(dict));
	}
	
	/**
	 * 修改字典数据
	 * @param dict
	 * @return
	 */
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody SysDictData dict) {
		logger.info("新增字典数据,dict:{}",dict);
		dict.setUpdateBy(getUsername());
		return toAjax(sysDictDataService.updateDictData(dict));
	}
	
	/**
	 * 删除字典数据
	 * @param dictCode
	 * @return
	 */
	@DeleteMapping(value = "/{dictCode}")
	public AjaxResult remove(@PathVariable Long dictCode) {
		logger.info("新增字典数据,dict:{}",dictCode);
		sysDictDataService.deleteDictDataById(dictCode);
		return AjaxResult.success();
	}
}
